/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 网站服务平台
 * 创建日期 : 2016-11-30
 * 修改历史 :
 * 1. [2016-11-30]创建文件 by dengcanyang
 */
package com.sinosoft.sinopay.service.paychannel.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.sinocloud.platform.common.web.pagequery.PageGrid;
import com.sinosoft.sinocloud.platform.common.web.pagequery.PageQueryModel;
import com.sinosoft.sinopay.paychannel.dto.PayThirdpayDTO;
import com.sinosoft.sinopay.service.paychannel.biz.integration.dao.PayChannelPageMapper;
import com.sinosoft.sinopay.service.paychannel.biz.service.PayChannelConfigService;
import com.sinosoft.sinopay.service.paychannel.generated.mapper.PayThirdpayMainMapper;
import com.sinosoft.sinopay.service.paychannel.generated.model.PayThirdpayMain;
import com.sinosoft.sinopay.service.paychannel.generated.model.PayThirdpayMainExample;

/**
 * 【支付渠道配置】
 * @author dengcanyang
 * @version V1.0
 * @date 2016-11-29 下午18:28
 */
@Service
public class PayChannelConfigServiceImpl implements PayChannelConfigService {
	
	@Autowired
	private PayThirdpayMainMapper payThirdpayMainMapper;
	
	@Autowired
	private PayChannelPageMapper payChannelPageMapper;
	@Override
	public PayThirdpayDTO selectPayThirdpayInfoBySid(Integer cSid) {
		//调用
		PayThirdpayMain tPayThirdpayMain = payThirdpayMainMapper.selectByPrimaryKey(cSid);
		//DTO 转换
		PayThirdpayDTO tPayThirdpayDTO = new PayThirdpayDTO();
		tPayThirdpayDTO.setSid(tPayThirdpayMain.getSid());
		tPayThirdpayDTO.setThirdpayCode(tPayThirdpayMain.getThirdpayCode());
		tPayThirdpayDTO.setThirdpayName(tPayThirdpayMain.getThirdpayName());  //DTO 具体转换过程
		tPayThirdpayDTO.setThirdpayType(tPayThirdpayMain.getThirdpayType());
		tPayThirdpayDTO.setStatus(tPayThirdpayMain.getStatus());
		tPayThirdpayDTO.setModifyDate(tPayThirdpayMain.getModifyDate());
		return tPayThirdpayDTO;
	}

	@Override
	public boolean insertPayChannel(PayThirdpayDTO cPayThirdpayDTO) {
		PayThirdpayMain tPayThirdpayMain = new PayThirdpayMain();
		tPayThirdpayMain.setThirdpayCode(cPayThirdpayDTO.getThirdpayCode());
		tPayThirdpayMain.setThirdpayName(cPayThirdpayDTO.getThirdpayName());
		tPayThirdpayMain.setThirdpayType(cPayThirdpayDTO.getThirdpayType());
		tPayThirdpayMain.setStatus(cPayThirdpayDTO.getStatus());
		//补充日志信息
		tPayThirdpayMain.setCreatorId(cPayThirdpayDTO.getCreatorId());
		tPayThirdpayMain.setCreateDate(new Date()); //获取系统当前时间
		tPayThirdpayMain.setModifierId(cPayThirdpayDTO.getModifierId());
		tPayThirdpayMain.setModifyDate(new Date());
		int resultFlag = 0;
		try {
			resultFlag= payThirdpayMainMapper.insert(tPayThirdpayMain); //新增支付渠道
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (1==resultFlag) { 
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean updatePayChannelInfo(PayThirdpayDTO cPayThirdpayDTO) {
		PayThirdpayMain tPayThirdpayMain = new PayThirdpayMain();
		tPayThirdpayMain.setSid(cPayThirdpayDTO.getSid());
		tPayThirdpayMain.setThirdpayCode(cPayThirdpayDTO.getThirdpayCode());
		tPayThirdpayMain.setThirdpayName(cPayThirdpayDTO.getThirdpayName());
		tPayThirdpayMain.setThirdpayType(cPayThirdpayDTO.getThirdpayType());
		tPayThirdpayMain.setStatus(cPayThirdpayDTO.getStatus());
		tPayThirdpayMain.setModifierId(cPayThirdpayDTO.getModifierId());
		tPayThirdpayMain.setModifyDate(new Date());
		//条件修改 支付配置信息
		PayThirdpayMainExample tPayThirdpayMainExample = new PayThirdpayMainExample();
		tPayThirdpayMainExample.createCriteria().andSidEqualTo(cPayThirdpayDTO.getSid());
		int updateFlag = 0;
		try {
			updateFlag = payThirdpayMainMapper.updateByExampleSelective(tPayThirdpayMain, tPayThirdpayMainExample);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (1==updateFlag) {  //受影响行数
			return true;
		}else {
			return false;
		}
	}

	@Override
	public PageGrid selectPayChannelPageList(PageQueryModel cPageQueryModel) {
		PageGrid tPageGrid = payChannelPageMapper.findPageGrid(cPageQueryModel);
		return tPageGrid;
	}

	@Override
	public PayThirdpayDTO selectPayChannelDetailsBySid(String cSid) {
		//获取返回值
		PayThirdpayMain tPayThirdpayMain = payThirdpayMainMapper.selectByPrimaryKey(Integer.parseInt(cSid));
		//转换
		PayThirdpayDTO tPayThirdpayDTO = new PayThirdpayDTO();
		tPayThirdpayDTO.setThirdpayCode(tPayThirdpayMain.getThirdpayCode());
		tPayThirdpayDTO.setThirdpayName(tPayThirdpayMain.getThirdpayName());  //DTO 具体转换过程
		tPayThirdpayDTO.setThirdpayType(tPayThirdpayMain.getThirdpayType());
		tPayThirdpayDTO.setStatus(tPayThirdpayMain.getStatus());
		tPayThirdpayDTO.setCreateDate(tPayThirdpayMain.getCreateDate());
		tPayThirdpayDTO.setModifyDate(tPayThirdpayMain.getModifyDate());
		
		
		return tPayThirdpayDTO;
	}

	@Override
	public boolean deletePayChannelInforBySid(String cSid) {
		PayThirdpayMain tPayThirdpayMain = payThirdpayMainMapper.selectByPrimaryKey(Integer.parseInt(cSid));
		tPayThirdpayMain.setStatus("0"); //逻辑删除
		//条件修改 支付配置信息
		PayThirdpayMainExample tPayThirdpayMainExample = new PayThirdpayMainExample();
		tPayThirdpayMainExample.createCriteria().andSidEqualTo(Integer.parseInt(cSid));
		int delFlag = 0;
		try {
			delFlag = payThirdpayMainMapper.updateByExampleSelective(tPayThirdpayMain, tPayThirdpayMainExample);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (1==delFlag) {   //受影响行数
			return true;
		}else {
			return false;
		}
	}

	@Override
	public List<PayThirdpayDTO> selectThirdpayNameList() {
		PayThirdpayDTO cPayThirdpayDTO = new PayThirdpayDTO();
		cPayThirdpayDTO.setStatus("1"); //查询状态有效的支付渠道信息
		PayThirdpayMainExample tPayThirdpayMainExample = new PayThirdpayMainExample();
		tPayThirdpayMainExample.createCriteria().andStatusEqualTo(cPayThirdpayDTO.getStatus());
		List<PayThirdpayMain> tPayThirdpayMainList = payThirdpayMainMapper.selectByExample(tPayThirdpayMainExample);
		List<PayThirdpayDTO> tPayThirdpayDTOList = new ArrayList<PayThirdpayDTO>();
		for (int i = 0; i < tPayThirdpayMainList.size(); i++) {   //返回状态有效的支付渠道名称信息
			PayThirdpayDTO cPayThirdpayDTO1 = new PayThirdpayDTO();
			cPayThirdpayDTO1.setThirdpayCode(tPayThirdpayMainList.get(i).getThirdpayCode());
			cPayThirdpayDTO1.setThirdpayName(tPayThirdpayMainList.get(i).getThirdpayName());
			tPayThirdpayDTOList.add(cPayThirdpayDTO1);
		}
		return tPayThirdpayDTOList;
	}

	@Override
    public PayThirdpayDTO queryPayThirdpayMainByThirdpayCode(String thirdpayCode) {
	    PayThirdpayMainExample tPayThirdpayMainExample = new PayThirdpayMainExample();
	    tPayThirdpayMainExample.createCriteria().andThirdpayCodeEqualTo(thirdpayCode);
	    List<PayThirdpayMain> tPayThirdpayList = payThirdpayMainMapper.selectByExample(tPayThirdpayMainExample);
	    PayThirdpayDTO tPayThirdpayDTO = new PayThirdpayDTO();
	    for (int i = 0; i < tPayThirdpayList.size(); i++) {   //一个商户编码 对应一条商户信息
	        tPayThirdpayDTO.setSid(tPayThirdpayList.get(i).getSid());
	        tPayThirdpayDTO.setThirdpayCode(tPayThirdpayList.get(i).getThirdpayCode());
	        tPayThirdpayDTO.setThirdpayName(tPayThirdpayList.get(i).getThirdpayName());
	        tPayThirdpayDTO.setThirdpayType(tPayThirdpayList.get(i).getThirdpayType());
	        tPayThirdpayDTO.setStatus(tPayThirdpayList.get(i).getStatus());
        }
        return tPayThirdpayDTO;
    }

}
